package com.yljk.yljkproject.modelcallback;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.yilijk.base.AppBase;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.NetAttestationUtils;
import com.yilijk.base.utils.ActivityManagerUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.live.common.utils.MediaCountyManager;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.constants.UrlApi;
import com.yljk.servicemanager.interfaceapi.MineBackApi;
import com.yljk.yljkproject.ui.CenterActivity;
import com.yljk.yljkproject.utils.ReactUtils;

/**
 * create by shicheng
 *
 * 我的模块回调实现
 * mine module interface callback
 */
@Route(path = RouterApi.MINEBACKIMPL)
public
class MineBackImpl implements MineBackApi {
    @Override
    public void init(Context context) {

    }


    @Override
    public void onListClick(Context context, String pluginId, String compontName) {
        ReactUtils.INSTANCE.push(context,pluginId,compontName);
    }

    @Override
    public void onListClick2(Context context, String pluginId, String compontName, Bundle bundle) {
        ReactUtils.INSTANCE.push(context, pluginId, compontName,bundle);
    }

    @Override
    public void onCancelLogout(Activity activity,Context context,boolean is401) {
        if (is401){
            //删除本地
            //成功删除本地缓存数据
            boolean b = SharedPreferencesUtils.getInstance().removeAll(AppBase.getApplication());
            if (b){
                NetAttestationUtils.getInstance().setSALT("");
                ActivityManagerUtils.finishAllActivity();
                Intent intent = new Intent(activity, CenterActivity.class);
                intent.putExtra("isLogout",true);
                activity.startActivity(intent);
                activity.finish();
            }
            return;
        }
        //退出服务器
        HttpUtils.post(UrlApi.mine_lagout,new HttpCallBack<String>(){
            @Override
            public void onSuccess(String result, int code) {
                   if (code == 200){
                       //视频直播退出
                       MediaCountyManager.getInstance().appLogout();
                       //退出环信
                       //IMChat退出
                       EMClient.getInstance().logout(true, new EMCallBack() {
                           @Override
                           public void onSuccess() {
                               // TODO Auto-generated method stub
//                                    Toast.makeText(activity, "退出账号成功", Toast.LENGTH_SHORT).show();
                           }

                           @Override
                           public void onProgress(int progress, String status) {
                               // TODO Auto-generated method stub

                           }

                           @Override
                           public void onError(int code, String message) {
                               // TODO Auto-generated method stub

                           }
                       });
                       //删除本地
                       //成功删除本地缓存数据
                       boolean b = SharedPreferencesUtils.getInstance().removeAll(AppBase.getApplication());
                       if (b){
                           NetAttestationUtils.getInstance().setSALT("");
                           Intent intent = new Intent(activity, CenterActivity.class);
                           intent.putExtra("isLogout",true);
                           ActivityManagerUtils.finishAllActivity();
                           activity.startActivity(intent);
                       }
                   }
            }

            @Override
            public void onFailure(String failureMsg, int statusCode) {

            }
        });

    }

    @Override
    public void onCancelLogout(Activity activity, Context context) {
        onCancelLogout(activity,context,false);
    }

    @Override
    public void onClickjumpPersionInfoPage(Context context, String pluginId, String compontName) {
        ReactUtils.INSTANCE.push(context,pluginId,compontName);
    }

    @Override
    public void toMediaPageByUrl(String urlString) {
        MediaCountyManager.getInstance().pageClick(urlString);
    }
    
}
